﻿using System;
using System.Runtime.Serialization;
using System.Web.Mvc;

namespace ManagedUIFlow.Model
{
    [Serializable]
    public class ManagedFlow : ISerializable
    {
        private const string FlowKey = "Key";
        private const string NameKey = "Name";
        private const string CurrentNavigationKey = "CurrentNavigation";
        private const string RedirectKey = "RedirectKey";
        private const string RedirectUrlKey = "RedirectUrlKey";
        private const string FlowDataKey = "FlowData";
        private const string ConversationDataKey = "ConversationData";
        private const string ViewDataModelKey = "ViewDataModel";

        public ManagedFlow(string name)
            : this(Guid.NewGuid(), name, string.Empty, RedirectBehavior.Auto, string.Empty)
        {
            
        }

        public ManagedFlow(string name, string currentNavigation)
            : this(Guid.NewGuid(), name, currentNavigation, RedirectBehavior.Auto, string.Empty)
        {

        }

        public ManagedFlow(object key, string name, string currentNavigation, RedirectBehavior redirectBehavior, string redirectUrl)
        {
            Key = key;
            Name = name;            
            Redirectable = redirectBehavior;
            RedirectUrl = redirectUrl;
            CurrentNavigation = currentNavigation;            
            ViewData = new ViewDataDictionary();
            TempData = new TempDataDictionary();
            FlowData = new ManagedFlowDataDictionary();
            ConversationData = new ManagedFlowDataDictionary();            
        }

        public ManagedFlow(SerializationInfo info, StreamingContext context)
        {
            Key = info.GetValue(FlowKey, typeof (object));
            Name = info.GetString(NameKey);
            CurrentNavigation = info.GetString(CurrentNavigationKey);
            Redirectable = (RedirectBehavior)info.GetValue(RedirectKey, typeof(RedirectBehavior));
            RedirectUrl = info.GetString(RedirectUrlKey);
            ConversationData = (ManagedFlowDataDictionary)info.GetValue(ConversationDataKey, typeof(ManagedFlowDataDictionary));
            FlowData = (ManagedFlowDataDictionary)info.GetValue(FlowDataKey, typeof(ManagedFlowDataDictionary));
            ViewData = new ViewDataDictionary
                           {
                               Model = info.GetValue(ViewDataModelKey, typeof (object))
                           };
            TempData = new TempDataDictionary();
        }

        public object Key { get; private set; }

        public string Name { get; private set; }       

        public RedirectBehavior Redirectable { get; private set; }

        public string RedirectUrl { get; private set; }

        public string CurrentNavigation { get; private set; }

        public ViewDataDictionary ViewData { get; set; }

        public TempDataDictionary TempData { get; set; }

        public ManagedFlowDataDictionary FlowData { get; set; }

        public ManagedFlowDataDictionary ConversationData { get; set; }                     

        public virtual void SetNavigation(string navigationName)
        {
            CurrentNavigation = navigationName;
        }

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue(FlowKey, Key);
            info.AddValue(NameKey, Name);
            info.AddValue(CurrentNavigationKey, CurrentNavigation);
            info.AddValue(RedirectKey, Redirectable);
            info.AddValue(RedirectUrlKey, RedirectUrl);
            info.AddValue(ConversationDataKey, ConversationData);
            info.AddValue(FlowDataKey, FlowData);
            info.AddValue(ViewDataModelKey, ViewData.Model);
        }
    }
}